<template>
  <div class="page-wrapper">
    <div class="header-wrapper">
      <h2 class="top">
        <van-icon class="back-icon" @click="fanhui" name="arrow-left" size="30px" />
        <span class="title-text">团队主页</span>
      </h2>
    </div>
    <!-- 团队信息 -->
    <div class="tuan-dui" v-if="doctorTeams">
      <div class="tu-pian">
        <img :src="doctorTeams.imageUrl" alt="文章图片" />
      </div>
      <div class="tuan-dui-text">
        <span class="tuan-dui-text-title">{{doctorTeams.teamName}}</span><br>
        <span class="jigo">所属机构：{{medicalInstitution.institutionName}}</span><br>
        <span class="jigo">签约人数：<span style="color: #388CF8">{{doctorTeams.signUpCount}}</span></span><br>
        <van-rate color="#FFD21E"
                  v-model="value"
                  allow-half
                  void-icon="star"
                  void-color="#eee"
        />
        <span class="ping-fen">{{doctorTeams.rating}}</span>
      </div>
      <div class="anniu">
        <button class="but1">慢病管理</button>
        <button class="but2">中医服务</button>
        <button class="but3">老年人管理</button>
      </div>
    </div>
    <h2 class="text-yi">团队医生</h2>
    <!-- 医生信息循环展示 -->
    <div class="yi-sheng" v-if="doctors.length > 0">
      <div class="xiao-yi" v-for="(doctor, index) in doctors" :key="index">
        <div class="tu-pian1">
          <img :src="doctor.imageUrl" alt="文章图片" />
        </div>
        <div class="yishi-div" @click="jinqu1(doctor.id)">
          <!-- 假设医生对象有 name 和 position 属性，根据实际情况调整 -->
          <span class="yi-sheng-text">{{doctor.doctorName}}</span><span class="quanke">{{doctor.position}}</span>
          <br><span class="quanke1">签到人数: {{doctor.signUpCount}}</span>
        </div>
        <div class="aaaaa">
          <el-button type="primary">签约</el-button>
        </div>
      </div>
    </div>
    <h2 class="text-yi">服务包</h2>
    <!-- 医生信息循环展示 -->
    <div class="yi-sheng" v-if="doctors.length > 0">
      <div class="xiao-yi" v-for="(servicePackage, index) in servicePackages" :key="index" @click="jinqu(servicePackage.id)">
        <div class="tu-pian1">
          <img :src="servicePackage.serviceItems" alt="文章图片" />
        </div>
        <div class="yishi-div" >
          <!-- 假设医生对象有 name 和 position 属性，根据实际情况调整 -->
          <span class="yi-sheng-text">{{servicePackage.packageName}}</span><br>
          <button class="but11">{{servicePackage.targetPopulation}}</button>
        </div>
        <div class="aaaaa">
          <span style="color: #FA746B">￥{{servicePackage.price}}</span>
        </div>
      </div>
    </div>
    <!-- 数据加载提示 -->
    <div v-if="isLoading" class="loading">数据加载中，请稍候...</div>
    <!-- 数据为空提示 -->
    <div v-if="!isLoading && doctors.length === 0" class="no-data">暂无医生信息</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: 4,
      teamId:null,
      doctorTeams: {},
      medicalInstitution: {},
      doctors: [],
      servicePackages:[],
      isLoading: true // 数据加载状态
    };
  },
  methods: {
    findAll() {
      this.isLoading = true; // 开始加载数据
      this.axios.get(`http://localhost:10086/api/doctor/doctor_team/getDoctorTeamByTeamId/${this.teamId}`)
          .then(res => {
            console.log('请求响应:', res);
            if (res.data.code === 200) {
              this.value = res.data.data.doctorTeam.rating;
              this.doctorTeams = res.data.data.doctorTeam;
              this.medicalInstitution = res.data.data.medicalInstitution;
              this.servicePackages = res.data.data.servicePackages;
              this.doctors = res.data.data.doctors;
              console.log('医生数据:', this.doctors); // 打印医生数据，检查是否有 id 属性
            } else {
              this.$message.error("获取医生团队失败");
            }
          })
          .catch(error => {
            console.error('请求出错:', error);
            this.$message.error('网络请求出错，请检查网络或服务器状态');
          })
          .finally(() => {
            this.isLoading = false; // 数据加载完成
          });
    },
    fanhui() {
      this.$router.go(-1);
    },
    jinqu(servicePackageId) {
      localStorage.setItem('servicePackageId', servicePackageId);
      this.$router.push({
        path: '/servicePackage1',
        query: {
          servicePackageId: servicePackageId
        }
      });
    },
    jinqu1(doctorId) {
      console.log('点击医生 ID:', doctorId);
      localStorage.setItem('doctorId', doctorId);
      this.$router.push({
        path: '/',
        query: {
          doctorId: doctorId
        }
      });
    }
  },
  created() {
    const token = JSON.parse(localStorage.getItem("token"));
    if (!token) {
      this.$message.error('请先登录');
      setTimeout(() => {
        this.$router.push({ name: 'login' });
      }, 1000);
    }
    this.teamId = this.$route.query.teamId;
    // 设置全局默认请求头
    this.axios.defaults.headers.common['token'] = token;
    this.findAll();
  }
};
</script>

<style scoped>
/* 样式部分保持不变 */
</style>

<style scoped>
.aaaaa {
  float: left;
  width: 30px;
  height: 30px;
  margin-top: 30px;
}

.quanke1 {
  color: dimgrey;
  font-size: 12px;
}

.quanke {
  margin-left: 10px;
  color: dimgrey;
  font-size: 12px;
}

.yi-sheng-text {
  color: black;
}

.yishi-div {
  float: left;
  width: 200px;
  height: 50px;
  margin-top: 30px;
  margin-left: 15px;
}

.tu-pian1 img {
  width: 80px;
  height: 80px;
  border-radius: 20px;
}

.tu-pian1 {
  width: 80px;
  height: 80px;
  float: left;
  margin-left: 10px;
  margin-top: 10px;
}

.xiao-yi {
  width: 90%;
  height: 100px;
  margin-left: 20px;
  border-radius: 15px;
  box-shadow: 0 0 10px rgba(0, 128, 255, 0.2);
  background-color: #F7FAFE;
  margin-bottom: 10px; /* 添加间隔 */
}

.yi-sheng {
  width: 100%;
  height: auto; /* 高度自适应 */
}

.text-yi {
  margin-left: 10px;
  color: dimgrey;
}

.but1 {
  float: left;
  color: #44D6B5;
  width: 110px;
  height: 35px;
  margin-top: 20px;
  margin-left: 10px;
  border-radius: 8px;
  background-color: #D8F6ED;
  border: none;
}

.but11{
  color: #44D6B5;
  width: 130px;
  height: 25px;
  margin-left: 10px;
  border-radius: 15px;
  background-color: #D8F6ED;
  border: none;
}

.but2 {
  float: left;
  color: #717AF4;
  width: 110px;
  height: 35px;
  margin-top: 20px;
  margin-left: 10px;
  border-radius: 8px;
  background-color: #E2E4FD;
  border: none;
}

.but3 {
  float: left;
  color: #FA898E;
  width: 110px;
  height: 35px;
  margin-top: 20px;
  margin-left: 10px;
  border-radius: 8px;
  background-color: #FEE3E1;
  border: none;
}

.anniu {
  width: 100%;
  height: 80px;
  float: left;
}

.ping-fen {
  margin-left: 10px;
  color: #FFCD1A;
  font-size: 18px;
}

.jigo {
  color: dimgrey;
}

.tuan-dui-text {
  width: 200px;
  height: 100px;
  float: left;
  margin-left: 10px;
  margin-top: 40px;
}

.tuan-dui-text-title {
  font-size: 20px;
  color: black;
}

.tu-pian {
  width: 150px;
  height: 150px;
  float: left;
  margin-left: 10px;
  margin-top: 10px;
}

.tu-pian img {
  width: 150px;
  height: 150px;
  float: left;
  border-radius: 30px;
  box-shadow: 0 0 10px rgba(0, 128, 255, 0.2);
}

.tuan-dui {
  width: 100%;
  height: 230px;
  background-color: #F7FAFE;
}

.page-wrapper {
  min-height: 100vh;
  background-color: #F5F9FD;
}

.header-wrapper {
  background-color: white;
}

.top {
  display: flex;
  align-items: center;
  height: 50px;
  position: relative;
}

.back-icon {
  position: absolute;
  left: 10px;
}

.title-text {
  width: 100%;
  text-align: center;
}

img {
  /*background-color: red;*/
  width: 100%;
  height: 100%;
}

.loading {
  text-align: center;
  margin-top: 20px;
  color: #999;
}

.no-data {
  text-align: center;
  margin-top: 20px;
  color: #999;
}
</style>